﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Form1
{
    public partial class Form3 : Form
    {
        public TestData TestData { get; protected set; }
        public bool IsReset { get; protected set; } = false;

        public Form3()
        {
            InitializeComponent();
            InitDataListBox();
        }

        private void InitDataListBox()
        {
            dataListBox.Items.Clear();
            foreach (var fileName in DataManager.Instance.DataRoot.DataSet)
            {
                dataListBox.Items.Add(fileName);
            }
        }

        private void uiButton2_Click(object sender, EventArgs e)
        {
            if (dataListBox.SelectedItem == null || string.IsNullOrEmpty(dataListBox.SelectedItem as string))
            {
                UIMessageBox.Show("请先选择数据文件");
            }

            bool result = UIMessageBox.ShowAsk("是否确认删除");
            if (result)
            {
                bool deleteResult = DataManager.Instance.Delete(dataListBox.SelectedItem as string);
                if (deleteResult)
                {
                    InitDataListBox();
                    UIMessageBox.Show("删除成功！");
                }
                else
                {
                    UIMessageBox.Show("删除失败！请检查文件是否被占用");
                }
            }
        }

        private void uiButton1_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataListBox.SelectedItem == null || string.IsNullOrEmpty(dataListBox.SelectedItem as string))
                {
                    UIMessageBox.Show("请先选择数据文件");
                }

                bool result = UIMessageBox.ShowAsk("是否确认还原");
                if (result)
                {
                    TestData = DataManager.Instance.GetTestDataByFileName(dataListBox.SelectedItem as string);
                    if (TestData == null)
                    {
                        UIMessageBox.Show("还原数据文件失败！");
                        return;
                    }
                    else
                    {
                        IsReset = true;
                        this.Close();
                    }
                }
            }
            catch(Exception ex)
            {
                UIMessageBox.Show(ex.Message);
            }
        }
    }
}
